package com.zhao.commonUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Description: <br/>
 * Copyright (c) ，2021 ， 赵 <br/>
 * This program is protected by copyright laws. <br/>
 * Date： 2021/1/8 16:04
 *
 * @author 柒柒
 * @version : 1.0
 * @Update [序号][日期YYYY-MM-DD] [更改人姓名][变更描述]
 */

public class MyUtils {

    public static JSONArray getJsonArray(String jsnStr, String field){
        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        String jsnField = jsnObj.getString(field);
        JSONArray js = JSON.parseArray(jsnField);
        return js;
    }

    public static String getJsnField(String jsnStr, String field){
        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        String jsnField = jsnObj.getString(field);
        return jsnField;
    }

    public static String getJsnColField(String jsnStr, String colField){

        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        JSONObject outerObj = JSONObject.parseObject(jsnObj.getString("Values"));
        JSONObject innerObj = null;
        if (outerObj != null){
            innerObj = JSONObject.parseObject(outerObj.getString("After"));
        }
        if (innerObj != null){
            return innerObj.getString(colField);
        }else{
            return null;
        }
    }

    public static String getJsnColFieldBefore(String jsnStr, String colField){
        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        JSONObject outerObj = JSONObject.parseObject(jsnObj.getString("Values"));
        JSONObject innerObj = null;
        if (outerObj != null){
            innerObj = JSONObject.parseObject(outerObj.getString("Before"));
        }
        if (innerObj != null){
            return innerObj.getString(colField);
        }else{
            return null;
        }
    }

    public static String getSCJsnField(String jsnStr, String col1, String col2){

        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        JSONObject innerObj = JSONObject.parseObject(jsnObj.getString(col1));
        if (innerObj != null){
            return innerObj.getString(col2);
        }else{
            return null;
        }
    }

}
